/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.alarm.overview;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import twaver.Alarm;
import twaver.AlarmProbableCause;
import twaver.AlarmSeverity;
import twaver.AlarmTrendIndication;
import twaver.AlarmType;
import twaver.Dummy;
import twaver.Element;
import twaver.Link;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverUtil;
import twaver.base.enumerable.EnumerableManager;
import twaver.network.TNetwork;
import twaver.swing.TExpandPane;
import twaver.table.TAlarmOverview;
import twaver.table.TAlarmTable;
import twaver.table.TTableColumn;
import twaver.table.TTableNavigator;
import twaver.tree.TTree;

public class MainUI extends JSplitPane{
	
    int counter = 0;
    static Object[] causes = EnumerableManager.getEnumerables(AlarmProbableCause.class).toArray();
    static Object[] types = EnumerableManager.getEnumerables(AlarmType.class).toArray();
    static Object[] trends = EnumerableManager.getEnumerables(AlarmTrendIndication.class).toArray();
	
    TDataBox box = new TDataBox("My Box");
    TNetwork network = new TNetwork(box);
    TTree tree = new TTree(box);
    TAlarmOverview overview = new TAlarmOverview(box);
    JSplitPane rigthSplit = new JSplitPane();
    TAlarmTable alarmTable = new TAlarmTable(box);
    
	public MainUI(){
		double startAngle = 30;
        JPanel leftPanel = new JPanel(new BorderLayout());
        leftPanel.add(new JScrollPane(tree), BorderLayout.CENTER);
        TExpandPane overviewExpandPane = new TExpandPane(overview, TExpandPane.SOUTH, true, true); 
        leftPanel.add(overviewExpandPane, BorderLayout.SOUTH);
        
        this.setContinuousLayout(true);
        this.add(rigthSplit, JSplitPane.RIGHT);
        this.add(leftPanel, JSplitPane.LEFT);

        JPanel tablePanel = new JPanel(new BorderLayout());
        alarmTable.getTableModel().setPageRowSize(20);

        TTableNavigator navigator = new TTableNavigator(alarmTable.getTableModel(), new int[] {20, 30, 50, 0});
        tablePanel.add(navigator, BorderLayout.NORTH);
        tablePanel.add(new JScrollPane(alarmTable), BorderLayout.CENTER);

        rigthSplit.setOrientation(JSplitPane.VERTICAL_SPLIT);
        rigthSplit.add(network, JSplitPane.TOP);
        rigthSplit.add(tablePanel, JSplitPane.BOTTOM);

        tree.setEnsureVisibleOnSelected(true);

		network.setEnableDeleteWithKeyboard(true);

        this.setDividerLocation(200);
        rigthSplit.setDividerLocation(400);

        alarmTable.addColumn(new TTableColumn("key1").setEditable(true));
        alarmTable.addColumn(new TTableColumn("key2"));
        alarmTable.addColumn(new TTableColumn("key3"));
        alarmTable.addColumn(new TTableColumn("counter").setEditable(true).setJavaClass(Integer.class));

        alarmTable.addAlarmClickedActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Alarm alarm = (Alarm)e.getSource();
                Element element = box.getElementByID(alarm.getElementID());
                box.getSelectionModel().setSelection(element);
			}
        });
        alarmTable.addAlarmDoubleClickedActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Alarm alarm = (Alarm)e.getSource();
				AlarmUI.createFrameAlarmUI(alarm).setVisible(true);
			}
        });
        alarmTable.setConverseIncreaseOrder(true);

        initData(startAngle);
        tree.expandAll();

    }

    private void initData(double startAngle) {
        Node center = new Node("center");
        center.setName("center");
        center.setLocation(200, 200);
        center.setImage("/demo/resource/images/cloud.png");
        box.addElement(center);
        Dummy nodes = new Dummy("My Nodes");
        nodes.setName("My Nodes");
        box.addElement(nodes);
        Dummy links = new Dummy("My Links");
        links.setName("My Links");
        box.addElement(links);

        int count = 10;
        int r = 150;
        for (int i = 0; i < count; i++) {
            Node node = new Node("NODE" + i);
            node.setName("Computer" + i);
            node.setParent(nodes);
            double angle = Math.PI * 2 / count * i + startAngle;
            int x = center.getLocation().x + center.getWidth() / 2;
            x = x + (int) (r * Math.cos(angle));
            x = x - node.getWidth() / 2;
            int y = center.getLocation().y + center.getHeight() / 2;
            y = y + (int) (r * Math.sin(angle));
            y = y - node.getHeight() / 2;
            node.setLocation(x, y);
            box.addElement(node);

            Link link = new Link("C_TO_" + node.getID(), center, node);
            link.setParent(links);
            link.putLinkWidth(6);
            link.putLink3D(true);
            box.addElement(link);

        }
    }

    public void createAlarm() {
    	if(!network.isShowing()){
    		return;
    	}

        Element element = box.getRandomElement();
        AlarmSeverity severity = TWaverUtil.getRandomNonClearedSeverity();

        Alarm alarm = new Alarm(TWaverUtil.valueOf(counter++), element.getID(), severity);

        alarm.setAcked(TWaverUtil.getRandomBool());
        alarm.setAckTime(new Date());
        alarm.setAckUserID("User679");
        alarm.setAdditionalText("additionalText");
        alarm.setProbableCause( (AlarmProbableCause) causes[TWaverUtil.getRandomInt(causes.length)]);
        alarm.setAlarmType( (AlarmType) types[TWaverUtil.getRandomInt(types.length)]);
        alarm.setCleared(false);
        alarm.setTrendIndication( (AlarmTrendIndication) trends[TWaverUtil.getRandomInt(trends.length)]);
        alarm.setLastChangedTime(new Date());
        alarm.setClearedTime(new Date());
        alarm.setRaisedTime(new Date());
        alarm.setClearUserID("Clear user name");
        alarm.addComment("first comment");
        alarm.addComment("second comment");
        alarm.addCorrelatedAlarmID("ALM0008");
        alarm.addCorrelatedAlarmID("ALM0009");
        alarm.setProposedRepairAction("Junk it now");
        alarm.setSpecificProblem("no specific problem");

        alarm.putClientProperty("key1", "This is key1 value.");
        alarm.putClientProperty("key2", new Date());
        alarm.putClientProperty("key3", "test value.");
        alarm.putClientProperty("counter", TWaverUtil.valueOf(counter));

        box.getAlarmModel().addAlarm(alarm);

        alarm = alarmTable.getAlarmByOID(TWaverUtil.valueOf(1));
        if (alarm != null) {
            alarm.setAlarmSeverity(severity);
            alarm.putClientProperty("counter", TWaverUtil.valueOf(counter));
        }
    }

}